require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'



class TC_UploadFiles_test < Test::Unit::TestCase
  
    def startup
        $UserName ='User123'
        $UserPassword ='pass123'
        $UserEmail = 'user123@user.com'
        $FolderName = 'MainFolder'
        $FolderName1 = 'FirstFolder'
        $FolderName2 = 'SecondFolder'
        $FolderName3 = 'ThirdFolder'
        $FilePath1 = '../resources/Images/gallery_slideshow.gif'
        $FilePath2 = '../resources/Images/gallery_slideshow.gif'
        @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
     
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    def test_06_verifyLinks
        assert_equal $SITE_URL , ie.link(:text, 'Home').href
        assert_equal $SITE_URL+'blogs/', ie.link(:text, 'Blogs').href
        assert_equal $SITE_URL+'media/', ie.link(:text, 'Media').href
        assert_equal $SITE_URL+'forums/', ie.link(:text, 'Forums').href
        assert_equal $SITE_URL+'groups/', ie.link(:text, 'Groups').href
        assert_equal $SITE_URL+'controlpanel/default.aspx', ie.link(:text, 'Control Panel').href
      end
      
    def test_07_navigateToCreateUserAccount
        ie.goto($SITE_URL +'controlpanel/membership/UserAdd.aspx')
        assert(ie.text_field(:id,/username$/).exists?)
        ie.text_field(:id,/username$/).value = $UserName
        assert(ie.text_field(:id,/password$/).exists?)
        ie.text_field(:id,/password$/).value = $UserPassword
        password=ie.text_field(:id,/password$/).value
        assert(ie.text_field(:id,/password2$/).exists?)
        ie.text_field(:id,/password2$/).value =password
        assert(ie.text_field(:id,/email$/).exists?)
        ie.text_field(:id,/email$/).value = $UserEmail
        assert(ie.select_list(:id,/timezone$/).exists?)
        ie.select_list(:id,/timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.button(:name,/createButton$/).exists?)
        sleep 5
        ie.button(:name,/createButton$/).click
      end
      
    def test_08_navigateToUploadFiles
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        ie.text_field(:id, /username$/).value = $UserName
        ie.text_field(:id, /password$/).value = $UserPassword
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        sleep 5
        assert(ie.link(:text,'View all and upload files').exists?)
        ie.link(:text,'View all and upload files').click
        assert(ie.text_field(:id,/FolderName$/).exists?)
        ie.text_field(:id,/FolderName$/).value =$FolderName
        assert(ie.button(:id,/SaveFolder$/).exists?)
        ie.button(:id,/SaveFolder$/).click
        sleep 3
        ie.link(:text,$FolderName).click
        assert(ie.text_field(:id,/FolderName$/).exists?)
        ie.text_field(:id,/FolderName$/).value =$FolderName1
        sleep 3
        assert(ie.button(:id,/SaveFolder$/).exists?)
        ie.button(:id,/SaveFolder$/).click
        sleep 3
        assert(ie.text_field(:id,/FolderName$/).exists?)
        ie.text_field(:id,/FolderName$/).value =$FolderName2
        sleep 3
        assert(ie.button(:id,/SaveFolder$/).exists?)
        ie.button(:id,/SaveFolder$/).click
        sleep 3
        assert(ie.text_field(:id,/FolderName$/).exists?)
        ie.text_field(:id,/FolderName$/).value =$FolderName3
        sleep 3
        assert(ie.button(:id,/SaveFolder$/).exists?)
        ie.button(:id,/SaveFolder$/).click
        sleep 3
        ie.link(:text,$FolderName1).click
        sleep 3
        assert(ie.fileField(:id,/File$/).exists?)
        ie.fileField(:id,/File$/).set($FilePath1)
        sleep 3
        assert(ie.button(:id,/SaveFile$/).exists?)
        ie.button(:id,/SaveFile$/).click
        sleep 2
        assert(ie.link(:text,$FolderName).exists?)
        ie.link(:text,$FolderName).click
        sleep 2
         ie.link(:text,$FolderName2).click
        assert(ie.fileField(:id,/File$/).exists?)
        ie.fileField(:id,/File$/).set($FilePath2)
        assert(ie.button(:id,/SaveFile$/).exists?)
        ie.button(:id,/SaveFile$/).click
      end
      
      def  test_10_searchUser
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        test_04_populateLoginForm
        test_05_submitLoginForm
        sleep 5
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.select_list(:name,/PageList$/).exists?)
        ie.select_list(:name,/PageList$/).select('10')
        sleep 3
        assert(ie.button(:id,/searchButton$/).exists?)
        sleep 2
        ie.button(:id,/searchButton$/).click
      end
      
      def test_11_navigateToDeleteUser
        sleep 5
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               sleep 3
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 5
         ie.link(:text,'Next >').click
         sleep 5
         test_11_navigateToDeleteUser
       end
       sleep 5
       assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
       ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
       sleep 3
       assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
       sleep 3
       ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
       sleep 5
       ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
       assert(ie.text_field(:id,/searchText$/).exists?)
       ie.text_field(:id,/searchText$/).value ="*"
       sleep 3
       assert(ie.button(:id,/searchButton$/).exists?)
       sleep 2
       ie.button(:id,/searchButton$/).click  
       if(ie.link(:text,$UserName).exists?)
       puts 'User Not Deleted'
     end   
     end
     
    def test_99_Exit
        ie.close
    end
    
end
  